-- package.path = package.path ..';../../?.lua'
_G.EXPORT_ASSERT_TO_GLOBALS = true
package.path = package.path..';../../?.lua'
require('lib.luaunit')
-- require('buff/test/luaunit')
local buffLib = require('buff/buffLib')

_G.testBuffManager = {}

function testBuffManager:setUp()

end

function testBuffManager:test_add_delete_onlyOneBuff()

    local buffManager = buffLib.buffManager:New()
    local buff1 = buffLib.buff:New()

    buffManager:addBuff(buff1)
    assertTrue(buffManager:GetBuffCount() == 1)

    buff1:End()
    assertTrue(buffManager:GetBuffCount() == 0)

end

function testBuffManager:test_add_delete_buff_PostionRight()

    local buffManager = buffLib.buffManager:New()
    local buff1 = buffLib.buff:New()
    local buff2 = buffLib.buff:New()


    buffManager:addBuff(buff1)
    buffManager:addBuff(buff2)
    assertTrue(buffManager:GetBuffCount() == 2)

    buff1:End()
    assertTrue(buffManager:GetBuffCount() == 1)
    assertTrue(buffManager.tbBuffs[1] == buff2)

end

function testBuffManager:test_getEffectIndex()

    local buffManager = buffLib.buffManager:New()
    local buff1 = buffLib.buff:New(10001, 1)
    local buff2 = buffLib.buff:New(10001, 2)
    local buff3 = buffLib.buff:New(10002, 1)
    local buff4 = buffLib.buff:New(10002, 2)
    local buff5 = buffLib.buff:New(10101, 1)
    local buff6 = buffLib.buff:New(10101, 2)
    local buff7 = buffLib.buff:New(10102, 1)
    local buff8 = buffLib.buff:New(10102, 2)

    assertEquals(buffManager:getEffectIndex(buff1), nil)

    buffManager:addBuff(buff1)
    buffManager:addBuff(buff2)
    buffManager:addBuff(buff3)
    buffManager:addBuff(buff4)
    buffManager:addBuff(buff5)
    buffManager:addBuff(buff6)
    buffManager:addBuff(buff7)
    buffManager:addBuff(buff8)

    assertEquals(buffManager:getEffectIndex(buff1), 1)
    assertEquals(buffManager:getEffectIndex(buff2), 2)
    assertEquals(buffManager:getEffectIndex(buff3), 3)
    assertEquals(buffManager:getEffectIndex(buff4), 4)
    assertEquals(buffManager:getEffectIndex(buff5), 5)
    assertEquals(buffManager:getEffectIndex(buff6), 6)
    assertEquals(buffManager:getEffectIndex(buff7), 7)
    assertEquals(buffManager:getEffectIndex(buff8), 8)

end

function testBuffManager:test_buffRelationWeight()
    local buffManager = buffLib.buffManager:New()
    local buff1 = buffLib.buff:New(10001, 1)
    local buff2 = buffLib.buff:New(10001, 1)

    assertEquals(buffManager:getRelationWight(buff1, buff2), 7)

    local buff3 = buffLib.buff:New(10103, 1)
    local buff4 = buffLib.buff:New(10102, 2)
    assertEquals(buffManager:getRelationWight(buff3,buff4), 4)

    assertEquals(buffManager:getRelationWight(buff4,buff1), 0)
end

function testBuffManager:test_addBuffWithRule_notToReplace()
    local buffManager = buffLib.buffManager:New()
    local buff1 = buffLib.buff:New(10001, 1)
    buffManager:addBuff(buff1)
    local buffToAdd = buffLib.buff:New(10001, 1)
    local index = 1

    buffManager:addBuffWithRule(buffToAdd, buffLib.BuffChangeRulecfg.NotToReplace, index)
    assertTrue(buffManager.tbBuffs[1] == buff1)
end


function testBuffManager:test_addBuffWithRule_timeAdd()
    local buffManager = buffLib.buffManager:New()
    local buff1 = buffLib.buff:New(10001, 1, 10000)
    buffManager:addBuff(buff1)
    local buffToAdd = buffLib.buff:New(10001, 1, 1000)
    local index = 1

    buffManager:addBuffWithRule(buffToAdd, buffLib.BuffChangeRulecfg.TimeAdd, index)
    assertEquals(buffManager.tbBuffs[index]:getLiveTime(), 11000)

    buff1:setLiveTime(1000)
    buffToAdd:setLiveTime(1000)
    buffManager:addBuffWithRule(buffToAdd, buffLib.BuffChangeRulecfg.TimeAdd, index)
    assertEquals(buffManager.tbBuffs[index]:getLiveTime(), 2000)
end

function testBuffManager:test_addBuffWithRule_levelReplace()
    local buffManager = buffLib.buffManager:New()
    local buff1 = buffLib.buff:New(10001, 1, 10000)
    buffManager:addBuff(buff1)
    local buffToAdd = buffLib.buff:New(10002, 1, 1000)
    local index = 1

    buffManager:addBuffWithRule(buffToAdd, buffLib.BuffChangeRulecfg.LevelReplace, index)
    assertEquals(buffManager.tbBuffs[index]:getLevel(), 2)
    assertTrue(buffManager.tbBuffs[index] == buffToAdd)
end

function testBuffManager:test_addBuffWithRule_valueAdd()
    local buffManager = buffLib.buffManager:New()
    local buff1 = buffLib.buff:New(10001, 1, 1000, 1)
    buffManager:addBuff(buff1)
    local buffToAdd = buffLib.buff:New(10001, 1, 3000, 5)
    local index = 1

    buffManager:addBuffWithRule(buffToAdd, buffLib.BuffChangeRulecfg.ValueAdd, index)
    assertEquals(buffManager.tbBuffs[index]:getLiveTime(), 3000)
    assertEquals(buffManager.tbBuffs[index]:getEffectTimes(),6)
end

function testBuffManager:test_addBuffWithRule_buffAdd()
    local buffManager = buffLib.buffManager:New()
    local buff1 = buffLib.buff:New(10001, 1, 1000, 1)
    buffManager:addBuff(buff1)
    local buffToAdd = buffLib.buff:New(10001, 2, 3000, 5)
    local index = 1

    buffManager:addBuffWithRule(buffToAdd, buffLib.BuffChangeRulecfg.BuffAdd, index)
    assertTrue(buffManager.tbBuffs[index+1], buffToAdd)
    assertEquals(buffManager:GetBuffCount(), 2)
end

_G.testBuff ={}


_G.testBuffOperationAdd = {} --class
    function testBuffOperationAdd:setUp()
        -- set up tests
        self.args = 1
        -- print( 'testBuffOperationAdd:setUp' )
    end

    function testBuffOperationAdd:tearDown()
        -- some tearDown() code if necessary
        -- print( 'testBuffOperationAdd:tearDown' )
    end

    function testBuffOperationAdd:testFunctionName()
        -- arrange
        -- act
        -- assert
        assertEquals( self.args, 1 )
        self.args = 2
    end

    function testBuffOperationAdd:testSetUpPairOne()
        -- arrange
        -- act
        -- assert
        self.args = 2
        assertEquals( self.args, 2 )
    end

    function testBuffOperationAdd:testSetUpPairTwo()
        -- arrange
        -- act
        -- assert
        assertEquals( self.args, 1 )
    end




-- LuaUnit:run()
local lu = LuaUnit.new()
lu:setOutputType('tap')
lu:runSuite()
-- os.exit(lu:runSuite())
